﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WebDTO;
using WebIService;
using WebService.Entities;

namespace WebService
{
    public class JobService : IJobService
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public bool Add(string name, string code)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> jo = new BaseService<Job>(db);
                Job model = new Job()
                {
                    Name = name,
                    Code = code,
                    Id = Guid.NewGuid().ToString(),
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsDel = 1
                };

                return jo.Add(model);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(string id)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> job = new BaseService<Job>(db);
                var mode = db.Job.Find(id);
                mode.IsDel = 0;
                return job.Delete(mode);
            }
        }

        public List<JobDTO> GetJobList()
        {
            using (var db = new OAContext())
            {
                BaseService<Job> job = new BaseService<Job>(db);
                var list = job.GetLists(u => u.IsDel == 1).Select(m => new JobDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Code = m.Code,
                    IsDel = m.IsDel,
                    UpdateTime = m.UpdateTime,
                    CreateTime = m.CreateTime

                }).ToList();

                return list;
            }
        }

        public List<JobDTO> GetPageJobList(int limit, int offset, string Name, string Code, string sort, string Order, ref int rowCount)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> job = new BaseService<Job>(db);
                rowCount = 0;

                Expression<Func<Job, bool>> where = u => u.IsDel == 1;

                if (!String.IsNullOrWhiteSpace(Name))
                {
                    where = where.And(u => u.Name.Contains(Name));
                }
                if (!string.IsNullOrWhiteSpace(Code))
                {
                    where = where.And(u => u.Code.Contains(Code));
                }

                bool flag = Order == "asc" ? true : false;

                var orderby = PredicateExtensions.GetSortLambda<Job>(sort);

                var list = job.GetPagedList(offset, limit, ref rowCount, where, orderby, flag).Select(u => new JobDTO
                {
                    Id = u.Id,
                    Name = u.Name,
                    Code = u.Code,
                    IsDel = u.IsDel,
                    UpdateTime = u.UpdateTime,
                    CreateTime = u.CreateTime
                }).ToList();

                return list;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool up(string name, string code, string id)
        {
            using (var db = new OAContext())
            {
                BaseService<Job> bs = new BaseService<Job>(db);
                var model = db.Job.Find(id);
                model.Name = name;
                model.Code = code;
                return bs.Update(model);
            }
        }
    }
}
